package com.xinyue.game.gateway.common;

import com.xinyue.game.message.GameMessageType;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author 王广帅
 * @since 2023/7/25 21:30
 **/
public class GatewayResponseUtil {
    private static Logger logger = LoggerFactory.getLogger(GatewayResponseUtil.class);

    public static ByteBuf getResponseByteBuf(GatewayMessageWrapper gatewayMessageWrapper, int errorCode, String errorMsg) {
        GameGatewayMessageRequestHeader requestHeader = gatewayMessageWrapper.getHeader();
        GameGatewayMessageResponseHeader header = new GameGatewayMessageResponseHeader();
        header.setRequestUri(requestHeader.getRequestUri());
        header.setRequestId(requestHeader.getRequestId());
        header.setUserKey("-");
        header.setResponseTime(System.currentTimeMillis());
        header.setMessageType(GameMessageType.RESPONSE.getType());
        header.setErrorCode(errorCode);
        header.setErrorMsg(errorMsg);
        int size = 4 + header.getHeaderSize();
        ByteBuf byteBuf = Unpooled.buffer(size);
        byteBuf.writeInt(size);
        // 写入包头信息
        header.write(byteBuf);
        logger.debug("网关返回消息,size: {}, header: {}", size, header);
        return byteBuf;
    }
    

}
